package com.dxj.dog.apply.controller;

import com.dxj.common.core.domain.AjaxResult;
import com.dxj.common.utils.SecurityUtils;
import com.dxj.dog.admin.domain.DogAdmin;
import com.dxj.dog.admin.service.IDogAdminService;
import com.dxj.dog.processadoption.domain.DogAdoptionProcess;
import com.dxj.dog.processadoption.service.IDogAdoptionProcessService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/dog/apply")
public class ApplyDogController {
    @Autowired
    private IDogAdoptionProcessService dogAdoptionProcessService;

    @Autowired
    private IDogAdminService dogAdminService;

    /*
     *狗狗申请的接口
     *
     * @param dogAdmin 狗的ID
     * @return 申请成功 OR 申请失败
     */
    @PreAuthorize("@ss.hasPermi('dog:apply:get')")
    @GetMapping("/get")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult applyDog(DogAdmin dogAdmin) {
        //通过查询狗的id，把狗的状态改一下，然后加入过程表
        //校验dogId
        if (dogAdmin.getDogId() == null) {
            throw new IllegalArgumentException("dogId没找到，请联系管理员！");
        } else if (SecurityUtils.getLoginUser().getUser().getPhonenumber() == null ||
                "".equals(SecurityUtils.getLoginUser().getUser().getPhonenumber())) {
            throw new IllegalArgumentException("用户必须设置手机号码才能继续申请，方便工作人员进行联系。");
        } else {
            //修改状态为审批中
            //谁需要乐观锁？当然是更新操作
            //乐观锁开始：
            Long version = dogAdminService.selectDogAdminByDogId(dogAdmin.getDogId()).getVersion();
            dogAdmin.setDogAdoptionStatus("1");
            dogAdmin.setVersion(version);
            int changeRowNumber = dogAdminService.updateDogAdmin(dogAdmin);
            if (changeRowNumber == 0) {
                //乐观锁抢夺失败
                throw new RuntimeException("这款狗狗已经被别人抢啦！");
            }
            //同时插入过程表，过程表不需要加锁，插入新数据而已。
            DogAdoptionProcess adoptionProcess = new DogAdoptionProcess();
            adoptionProcess.setAdoptionUserId(SecurityUtils.getLoginUser().getUserId());
            adoptionProcess.setDogAdoptionStatus("1");
            adoptionProcess.setAdoptionUserNickName(SecurityUtils.getLoginUser().getUser().getNickName());
            adoptionProcess.setAdoptionUserPhone(SecurityUtils.getLoginUser().getUser().getPhonenumber());
            adoptionProcess.setDogId(dogAdmin.getDogId());
            adoptionProcess.setCreateTime(new Date());
            adoptionProcess.setCreateBy(SecurityUtils.getUsername());
            dogAdoptionProcessService.insertDogAdoptionProcess(adoptionProcess);
            //如果执行到这里没报错，则说明成功了
            return AjaxResult.success("恭喜你！申请已经成功，请等待工作人员审核！您也可以通过 流浪狗栏目 -> 我的申请  审核查看进度！");

        }
    }


}
